package org.wyn.mongodb.dao.impl;

import org.apache.log4j.Logger;
import org.wyn.mongodb.bean.User;
import org.wyn.mongodb.common.SearchBDProvider;
import org.wyn.util.CodeMD5;

import com.google.code.morphia.dao.BasicDAO;
/**
 * 用户信息mongodb dao层(实现)
 * @author Administrator
 *
 */
public class UserDao extends BasicDAO<User, String>{
	public UserDao(){
		super(SearchBDProvider.getInstance());
	}
	/**
	 * 验证账号密码是否正确
	 * @param userName
	 * @param pw
	 * @return
	 */
	public boolean isUserIn(String userName, String pw) {
		try {
			User user = findOne("userName",userName);
			if(user.getPassword().equalsIgnoreCase(CodeMD5.MD5_Code(pw))){
				return true;
			}
			return false;
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean isUserExist(String userName){
		try {
			User user = findOne("userName",userName);
			if(user != null && user.getUserName() != null){
				return true;
			}
			return false;
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	/**
	 * 根据userName得到realName
	 * @param userName
	 * @return 指定的userName不存在返回null
	 */
	public String getRealName(String userName){
		User user = findOne("userName", userName);
		if(null == user)
			return null;
		else
			return user.getRealName();
	}
	
	private static Logger log = Logger.getLogger(UserDao.class);
}
